<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Pembayaran_sekali extends CI_Controller {

	function __construct() {
        parent::__construct();
        $this->isLogin();
		$this->num_post=10;		
		$this->load->model("master/master_set_sekali"); 
		$this->load->model("master/master_tapel"); 	
		$this->load->model("master/master_siswa"); 
		$this->load->model("pembayaran_sekali_model"); 
	}

	function isLogin(){
		$login=$this->session->userdata('isLogin');
		if(! isset($login)||$login != true){
			$this->session->set_flashdata('error_login', 'Untuk Mengakses Admin Anda Harus Login!');
			redirect('login');	
		}
	}
	
	function index(){
		$data['main_view']='pembayaran_sekali/view';
		$this->load->view('index',$data);
	}
	
	function view_pembayaran(){
		$data['main_view']='pembayaran_sekali/view_pembayaran';
		$nis=$this->input->post('nis');
		if(empty($nis)){
			$nis=$this->uri->segment(3);
		}else{
			$nis=$this->input->post('nis');
		}
		//cek nis
		$cek_nis=$this->master_siswa->cek_nis($nis);
		if($cek_nis==false){
			$this->session->set_flashdata('pesan_error', 'NVA tidak ditemukan');
			redirect('pembayaran_sekali');
		}
		$data['detail_siswa']=$this->master_siswa->detail($nis);
		$cek_tagihan=$this->pembayaran_sekali_model->cek_tagihan($nis,$data['detail_siswa'][0]->id_tapel_masuk);
		if($cek_tagihan==true){
			//ada tagihan
			$data['status_aksi']="update";
			$data['list_tagihan']=$this->pembayaran_sekali_model->get_tagihan_belum_bayar($nis,$data['detail_siswa'][0]->id_tapel_masuk);
		}else{
			$data['status_aksi']="insert";
			$id_lembaga=$this->session->userdata('id_lembaga');
			$data['list_tagihan']=$this->master_set_sekali->get_detail($data['detail_siswa'][0]->id_tapel_masuk,$id_lembaga);
		}
		$data['list_lunas']=$this->pembayaran_sekali_model->list_pembayaran($nis);
		//echo $data['status_aksi'];
		//$data['list_tagihan']=$this->master_set_sekali->get_detail($data['detail_siswa'][0]->id_tapel_masuk);
		//print_r($data['list_tagihan']);
		$total_tagihan=0;
		$total_bayar=0;
		foreach($data['list_tagihan'] as $key=>$val){
			$data['list_tagihan'][$key]['jml_bayar']=$this->pembayaran_sekali_model->get_bayar($nis,$val['id_komponen']);
			$total_tagihan=+$val['nominal'];
		}
		foreach($data['list_tagihan'] as $key=>$val){
			$total_bayar=+$val['jml_bayar'];
		}
		//echo 'tot tagihan '.$total_tagihan.' tot bayar '.$total_bayar;
		$sisa=$total_tagihan-$total_bayar;
		if($sisa<=0){
			$data['status']='Lunas';
			//$data['disp_bayar']='none';
			$data['disp_bayar']='';
		}else{
			$data['status']='Belum Lunas, sisa Rp. '.number_format($sisa,0,",",".");
			$data['disp_bayar']='';
		}
		$data['nis']=$nis;
		$this->load->view('index',$data);
	}
	
	function proses_pembayaran(){
		$nis=$this->input->post('nis');
		$id_tapel=$this->input->post('id_tapel');
		$id_komp=$this->input->post('id_komp');
		$nominal=$this->input->post('nominal');
		$jml=count($id_komp);
		$status_aksi=$this->input->post('status_aksi');
		if($status_aksi=="insert"){
		for($i=0;$i<$jml;$i++){
			if(($nominal[$i]!="")||($nominal[$i]!="0")){
				$angka=str_replace(".","",$nominal[$i]);
				$angka=str_replace(",",".",$angka);
				$insert=$this->pembayaran_sekali_model->insert_tagihan($id_tapel,$id_komp[$i],$angka,$nis);
			}
		}  
		}else{
			if(!empty($id_komp)){
				foreach($id_komp as $key=>$val){
					if(($nominal[$key]!="")||($nominal[$key]!="0")){
						$angka=str_replace(".","",$nominal[$key]);
						$angka=str_replace(",",".",$angka);
						$insert=$this->pembayaran_sekali_model->update_tagihan($id_komp[$key],$angka);
					}
				}
				
			}
		}
		$this->session->set_flashdata('pesan', 'Setting Pembayaran Sukses Diinput');
		redirect('pembayaran_sekali');
	}
	
	function delete_pembayaran($id,$nis){
		$delete=$this->pembayaran_sekali_model->delete($id);
		$this->session->set_flashdata('pesan', 'Pembayaran berhasil dihapus');
		redirect('pembayaran_sekali/view_pembayaran/'.$nis);
	}
}

?>